<template>
  <div class="comment-manage">
    <div class="search-bar">
      <el-form :inline="true">
        <el-form-item label="关键词">
          <el-input
            v-model="searchKeyword"
            placeholder="活动标题或评价内容"
            clearable
          />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="status" placeholder="请选择状态" clearable>
            <el-option label="待审核" :value="0" />
            <el-option label="已通过" :value="1" />
            <el-option label="已拒绝" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table :data="commentList" border style="width: 100%" v-loading="loading">
      <el-table-column prop="id" label="ID"  />
      <el-table-column prop="userId" label="用户ID"  />
      <el-table-column prop="userName" label="用户昵称"  />
      <el-table-column prop="activityId" label="活动ID"  />
      <el-table-column prop="activityName" label="活动名称"  />

      <el-table-column prop="content" label="评价内容" show-overflow-tooltip />

      <el-table-column prop="createTime" label="评价时间" width="180">
        <template #default="scope">
          {{ formatDate(scope.row.createTime) }}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="260" fixed="right">
        <template #default="scope">
          <el-button
            type="success"
            size="small"
            @click="handleApprove(scope.row)"
            v-if="scope.row.status === 0"
          >
            通过
          </el-button>
          <el-button
            type="warning"
            size="small"
            @click="handleReject(scope.row)"
            v-if="scope.row.status === 0"
          >
            拒绝
          </el-button>
          <el-button
            type="danger"
            size="small"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
          <el-button
            type="primary"
            size="small"
            @click="handleViewDetails(scope.row)"
          >
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 拒绝理由弹窗 -->
    <el-dialog
      v-model="rejectDialogVisible"
      title="拒绝理由"
      width="30%"
    >
      <el-form>
        <el-form-item label="拒绝理由" label-width="100px">
          <el-input
            v-model="rejectReason"
            type="textarea"
            placeholder="请输入拒绝理由"
            rows="3"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="rejectDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmReject">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 查看详情弹窗 -->
    <el-dialog
      v-model="detailsDialogVisible"
      title="评价详情"
      width="50%"
    >
      <el-descriptions
        v-if="selectedComment"
        :column="2"
        border
      >
        <el-descriptions-item label="评价ID">{{ selectedComment.id }}</el-descriptions-item>
        <el-descriptions-item label="活动ID">{{ selectedComment.activityId }}</el-descriptions-item>
        <el-descriptions-item label="活动标题">{{ selectedComment.activityName }}</el-descriptions-item>
        <el-descriptions-item label="用户ID">{{ selectedComment.userId }}</el-descriptions-item>

        <el-descriptions-item label="创建时间">{{ formatDate(selectedComment.createTime) }}</el-descriptions-item>

        <el-descriptions-item label="评价内容" :span="2">{{ selectedComment.content }}</el-descriptions-item>
        <el-descriptions-item label="评价图片" :span="2" v-if="selectedComment.images">
          <div class="image-list">
            <el-image
              v-for="(img, index) in selectedComment.images.split(',')"
              :key="index"
              :src="img"
              :preview-src-list="selectedComment.images.split(',')"
              class="evaluation-image"
            />
          </div>
        </el-descriptions-item>
        <el-descriptions-item label="审核时间" v-if="selectedComment.auditTime">
          {{ formatDate(selectedComment.auditTime) }}
        </el-descriptions-item>
        <el-descriptions-item label="审核备注" v-if="selectedComment.auditRemark">
          {{ selectedComment.auditRemark }}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  getCommentList,
  approveComment,
  rejectComment,
  deleteComment,
  getCommentDetail
} from '@/api/admin/comment'

const searchKeyword = ref('')
const status = ref('')
const commentList = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const loading = ref(false)

// 拒绝理由相关
const rejectDialogVisible = ref(false)
const rejectReason = ref('')
const currentRejectId = ref(null)

// 详情相关
const detailsDialogVisible = ref(false)
const selectedComment = ref(null)

const formatDate = (dateString) => {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleString()
}

const getComments = async () => {
  loading.value = true
  try {
    const res = await getCommentList({
      pageNum: currentPage.value,
      pageSize: pageSize.value,
      keyword: searchKeyword.value,
      status: status.value !== '' ? status.value : null
    })
    if (res.code === 200) {
      commentList.value = res.data.records
      total.value = res.data.total
    } else {
      ElMessage.error(res.msg || '获取评价列表失败')
    }
  } catch (error) {
    console.error('获取评价列表失败:', error)
    ElMessage.error('获取评价列表失败')
  } finally {
    loading.value = false
  }
}

const handleSearch = () => {
  currentPage.value = 1
  getComments()
}

const resetSearch = () => {
  searchKeyword.value = ''
  status.value = ''
  currentPage.value = 1
  getComments()
}

const handleSizeChange = (size) => {
  pageSize.value = size
  getComments()
}

const handleCurrentChange = (page) => {
  currentPage.value = page
  getComments()
}

const handleApprove = async (row) => {
  try {
    ElMessageBox.confirm('确定要通过这条评价吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'info'
    }).then(async () => {
      const res = await approveComment(row.id)
      if (res.code === 200) {
        ElMessage.success('审核通过成功')
        getComments()
      } else {
        ElMessage.error(res.msg || '操作失败')
      }
    }).catch(() => {})
  } catch (error) {
    console.error('操作失败:', error)
    ElMessage.error('操作失败')
  }
}

const handleReject = (row) => {
  currentRejectId.value = row.id
  rejectReason.value = ''
  rejectDialogVisible.value = true
}

const confirmReject = async () => {
  if (!currentRejectId.value) return

  try {
    const res = await rejectComment(currentRejectId.value, rejectReason.value)
    if (res.code === 200) {
      ElMessage.success('已拒绝该评价')
      rejectDialogVisible.value = false
      getComments()
    } else {
      ElMessage.error(res.msg || '操作失败')
    }
  } catch (error) {
    console.error('操作失败:', error)
    ElMessage.error('操作失败')
  }
}

const handleDelete = (row) => {
  ElMessageBox.confirm('确定要删除这条评价吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      const res = await deleteComment(row.id)
      if (res.code === 200) {
        ElMessage.success('删除成功')
        getComments()
      } else {
        ElMessage.error(res.msg || '删除失败')
      }
    } catch (error) {
      console.error('删除失败:', error)
      ElMessage.error('删除失败')
    }
  }).catch(() => {})
}

const handleViewDetails = async (row) => {
  try {
    const res = await getCommentDetail(row.id)
    if (res.code === 200) {
      selectedComment.value = res.data
      detailsDialogVisible.value = true
    } else {
      ElMessage.error(res.msg || '获取详情失败')
    }
  } catch (error) {
    console.error('获取详情失败:', error)
    ElMessage.error('获取详情失败')
  }
}

onMounted(() => {
  getComments()
})
</script>

<style scoped>
.comment-manage {
  padding: 20px;
  min-height: calc(100vh - 120px);
  display: flex;
  flex-direction: column;
  background-color: #f0f2f5;
}
.search-bar {
  margin-bottom: 20px;
  background: #fff;
  padding: 15px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
.el-table {
  flex: 1;
  margin-bottom: 20px;
  height: auto;
}
/*.pagination {
  margin-top: 20px;
  background: #fff;
  padding: 10px;
  border-radius: 4px;
}*/
.pagination{
  float: right;
}
.evaluation-image {
  width: 100px;
  height: 100px;
  object-fit: cover;
  margin-right: 10px;
  border-radius: 4px;
}
.image-list {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

/* 使操作列按钮在一行显示 */
:deep(.el-table .cell) {
  white-space: nowrap;
}

:deep(.el-pagination) {
  float: right;
}

:deep(.el-table__fixed-right) {
  height: auto !important;
}
</style>
